package com.googlecode.jascii;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Description: 
 *              TdResult負責字串顯示,靠右靠左及置中,設定TD寬度
 *              使用範例 請參照TdResult.main 
 * @author Kim Lee
 */
public class TdResult {
//	TD的置中 置左 或置右
	public final static int TD_STYLE_LEFT = 0;
	public final static int TD_STYLE_MIDDLE = 1;
	public final static int TD_STYLE_MIDDLE_RIGHT_NOT_SPACE = 3;
	public final static int TD_STYLE_RIGHT = 2;
	public static final int TD_DATA_TYPE = 98;
	public static final int TD_ASCII_TYPE = 99;
	
    private int defaultLength;
    private List valueList=null;
    private String defaultStr = " ";
    private String value;
    private int style;
    private int tdTyle=1;
    private boolean notTrim = false;
	
	/**
	 * @param value
	 * @param defaultLength
	 * @param style
	 */
	public TdResult(String value, int defaultLength, int style) {
		this.defaultLength = defaultLength;
		this.style = style;
		if(value==null)value="";
		this.setValue(value.trim());
	}
	/**
	 * @param value
	 * @param defaultLength
	 * @param style
	 * @param notTrim
	 */
	public TdResult(String value, int defaultLength, int style,boolean notTrim) {
		this.defaultLength = defaultLength;
		this.style = style;
		this.notTrim = notTrim;
		if(value==null)value="";
		if(notTrim) this.setValue(value);
		else this.setValue(value.trim());
	}
	/**
	 * @param values
	 * @param defaultLength
	 * @param style
	 */
	public TdResult(String[] values, int defaultLength, int style) {
		this.defaultLength = defaultLength;
		this.style = style;
		if(values==null)values=new String[]{""};
		for(int i=0;i<values.length;i++)
		{
			if(values[i]==null)this.addValue("");
			else	this.addValue(values[i].trim());
		
		}
	}
	/**
	 * @param values
	 * @param defaultLength
	 * @param style
	 * @param notTrim
	 */
	public TdResult(String[] values, int defaultLength, int style,boolean notTrim) {
		this.defaultLength = defaultLength;
		this.style = style;
		this.notTrim = notTrim;
		if(values==null)values=new String[]{""};
		for(int i=0;i<values.length;i++)
		{
			if(values[i]==null)this.addValue("");
			else
			{
				if(notTrim)	this.addValue(values[i]);
				else this.addValue(values[i].trim());
			}
		
		}
	}
	/**
	 * @param value
	 * @param defaultLength
	 * @param style
	 * @param defaultStr
	 */
	
	public TdResult(String value, int defaultLength, int style,String defaultStr) {
		this.defaultLength = defaultLength;
		this.style = style;
		if(value==null)value="";
		this.defaultStr = defaultStr;
		this.setValue(value.trim());
	}
	
	/**
	 * @param values
	 * @param defaultLength
	 * @param style
	 * @param defaultStr
	 */
	public TdResult(String[] values, int defaultLength, int style,String defaultStr) {
		this.defaultLength = defaultLength;
		this.style = style;
		if(values==null)values=new String[]{""};
		this.defaultStr = defaultStr;
		for(int i=0;i<values.length;i++)
		{
			if(values[i]==null)this.addValue("");
			else
				this.addValue(values[i].trim());
			
		}
	}
	/**
	 * @return 傳回 defaultLength。
	 */
	public int getDefaultLength() {
		return defaultLength;
	}
	/**
	 * @param defaultLength 要設定的 defaultLength。
	 */
	public void setDefaultLength(int defaultLength) {
		this.defaultLength = defaultLength;
	}
	/**
	 * @return 傳回 defaultStr。
	 */
	public String getDefaultStr() {
		return defaultStr;
	}
	/**
	 * @param defaultStr 要設定的 defaultStr。
	 */
	public void setDefaultStr(String defaultStr) {
		this.defaultStr = defaultStr;
	}
	/**
	 * @return 傳回 style。
	 */
	public int getStyle() {
		return style;
	}
	/**
	 * @param style 要設定的 style。
	 */
	public void setStyle(int style) {
		this.style = style;
	}
	/**
	 * @return 傳回 value。
	 */
	public String getValue() {
		return value;
	}
	/**
	 * @param value 要設定的 value。
	 */
	public void setValue(String value) {
		if(value==null)value="";
		int byteSize = countDualChar( value)+value.length();
		int charSize = value.length();
		if(byteSize>defaultLength && defaultLength > 0) 
		{
			valueList = new ArrayList();
			char[] chars = value.toCharArray();
			StringBuffer charsb = new StringBuffer();
			int charsvByteSize = 0;
			for(int i=0;i<chars.length;i++)
			{
				int dualCount = countDualChar( String.valueOf(chars[i]))+1;
				charsvByteSize+=dualCount;
				if( defaultLength >= charsvByteSize )
				{
					
//					if(defaultLength== charsvByteSize &&((chars[i] > 'a' && chars[i]<'z') || (chars[i] > 'A' && chars[i]<'Z') ) && ((chars[i-1] > 'a' && chars[i-1]<'z') || (chars[i-1] > 'A' && chars[i-1]<'Z'))   ) 
//					{
//						charsb.append('-');
//						valueList.add(charsb.toString());
//						charsb.setLength(0);
//						charsvByteSize = dualCount;
//						charsb.append(chars[i]);
//					}
//					else 
					charsb.append(chars[i]);
					
				}else
				{
					
					valueList.add(charsb.toString());
					charsb.setLength(0);
					charsvByteSize = dualCount;
					charsb.append(chars[i]);
				}
			}
			if(charsb.length()!=0)
			valueList.add(charsb.toString());
			
			
			this.value =(String) valueList.get(0);
		}else 
		this.value = value;
	}
	public void addValue(String value) {
		if(value==null)value="";
		int byteSize = countDualChar( value)+value.length();
		int charSize = value.length();
		if(valueList==null)valueList = new ArrayList();
		if(byteSize>defaultLength && defaultLength > 0) 
		{
			
			char[] chars = value.toCharArray();
			StringBuffer charsb = new StringBuffer();
			int charsvByteSize = 0;
			for(int i=0;i<chars.length;i++)
			{
				int dualCount = countDualChar( String.valueOf(chars[i]))+1;
				charsvByteSize+=dualCount;
				if( defaultLength >= charsvByteSize )
				{
//					if(defaultLength== charsb.toString().getBytes().length && ((chars[i] > 'a' && chars[i]<'z') || (chars[i] > 'A' && chars[i]<'Z') ) && ((chars[i-1] > 'a' && chars[i-1]<'z') || (chars[i-1] > 'A' && chars[i-1]<'Z'))   ) 
//					{
//						charsb.append('-');
//						valueList.add(charsb.toString());
//						charsb.setLength(0);
//						charsb.append(chars[i]);
//					}
//					else 
					charsb.append(chars[i]);
					
				}else
				{
					valueList.add(charsb.toString());
					charsb.setLength(0);
					charsvByteSize = dualCount;
					charsb.append(chars[i]);
				}
			}
			if(charsb.length()!=0)
			valueList.add(charsb.toString());
			
			this.value =(String) valueList.get(0);
		}else 
		{
			valueList.add(value);
			this.value = (String) valueList.get(0);
		}
	}
	public TdResult getTdResult(int i)
	{
		if(i==1)return this;
		
		if(this.style == TdResult.TD_ASCII_TYPE) this.defaultStr = this.getValue();
		if(valueList==null)
		{
			if(this.defaultStr!=null)
				  return new TdResult( this.defaultStr, this.defaultLength,this.style);
			else return new TdResult( this.defaultStr, this.defaultLength,this.style,this.defaultStr);
		}
		if(i<=0)  throw new IndexOutOfBoundsException("over TdResult");
		if(i > valueList.size())
		{
			if(this.defaultStr!=null)
			  return new TdResult( this.defaultStr, this.defaultLength,this.style);
			else return new TdResult( this.defaultStr, this.defaultLength,this.style,this.defaultStr);
		}
		if(this.defaultStr!=null)
		return new TdResult( (String)valueList.get(i-1), this.defaultLength,this.style);
		else return new TdResult( (String)valueList.get(i-1), this.defaultLength,this.style,this.defaultStr);
	}
	public int getHeight()
	{
		if(valueList==null)return 1;
		else return valueList.size();
	}
	private boolean checkDualChar(String str)
	{
		
		return Pattern.compile("\\p{InCJKUnifiedIdeographs}").matcher(str).find();	
	}
	private int countDualChar(String str)
	{
		Matcher matcher = Pattern.compile("\\p{InCJKUnifiedIdeographs}").matcher(str);
		int i=0;
		while(matcher.find())
		{
			i++;
		}
		return i;
	}
	
	public String toString()
	{
		if(value==null)value="";
		int byteSize = countDualChar( value)+value.length();
		int charSize = value.length();
		int spaceSize =  defaultLength - byteSize;
		if(TD_STYLE_LEFT == style)
		{
			return value + this.getSpace(spaceSize);
		}else if(TD_STYLE_MIDDLE == style)
		{
			
			int middle = spaceSize/2;
			
			return this.getSpace(middle)+value+this.getSpace(spaceSize-middle);
		}else if(TD_STYLE_MIDDLE_RIGHT_NOT_SPACE == style)
		{
			int middle = spaceSize/2;
			return this.getSpace(middle)+value;
		}else
		{
			return this.getSpace(spaceSize)+value;
		}
	}
	
	private String getSpace(int size)
	{
		if(defaultStr==null) return "";
		if(this.tdTyle == TD_ASCII_TYPE) 
		{
		   StringBuffer sb = new StringBuffer();
		   for(int i=0;i<size;i++)
		   {
		   		sb.append(this.defaultStr);
		   }
		   return sb.toString();
		}else
		{
			if(defaultStr.getBytes().length>1)
			{
				StringBuffer sb = new StringBuffer();
				for(int i=0;i<size/2;i++)
			   {
			   		sb.append(this.defaultStr);
			   }
			   return sb.toString();
			}else
			{
				StringBuffer sb = new StringBuffer();
			   for(int i=0;i<size;i++)
			   {
			   		sb.append(this.defaultStr);
			   }
			   return sb.toString();
			}
		}
		
	   
	}
	
	public static void main(String[] args) {
		
		TdResult td = new TdResult("123 中文1",101,TD_STYLE_MIDDLE);
		System.out.println("======"+td+"======");
		TdResult td1 = new TdResult("123中文1",17,TD_STYLE_LEFT);
		System.out.println("======"+td1+"======");
		TdResult td2 = new TdResult("123中文1",17,TD_STYLE_RIGHT);
		System.out.println("======"+td2+"======");
	}
	
	
	
	/**
	 * @return Returns the tdTyle.
	 */
	public int getTdTyle() {
		return tdTyle;
	}
	/**
	 * @param tdTyle The tdTyle to set.
	 */
	public void setTdTyle(int tdTyle) {
		this.tdTyle = tdTyle;
	}
	public boolean isNotTrim() {
		return notTrim;
	}
}
